/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onlineshopcode;

/**
 *
 * @author Mattis
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.border.*;
import javax.swing.JOptionPane;




public final class GUI extends JPanel implements ActionListener{
    
      public   String userName ;
     public    String userPassword ;
 public JTextField userNameField;
 public JTextField passwordField;
 public JButton enter;
 public JButton buyShoes;
 public JButton exit;
  public JButton buyJacket;
  public JButton buyPillow;
  public JButton buyCandles;
  public JButton buySweater;
 JPanel loginPanel;
 JPanel productPanel;

 private ButtonGroup group;
 Container contentPane;
 MyFrame frame = new MyFrame();
 JLabel userNameText;
 JLabel passwordText;
 JLabel loginText;
 JLabel productText;
 JLabel bild;
 private JTextField shoes;
  private JTextField sweater;
   private JTextField jacket;
    private JTextField pillow;
     private JTextField scentedCandels;
   private JTextField shoesStock;
  private JTextField sweaterStock;
   private JTextField jacketStock;
    private JTextField pillowStock;
     private JTextField scentedCandelsStock;
       Database database = new Database();
   

 public GUI() {
     createLoginPanel();
        
 }

 public void createLoginPanel() {
     loginPanel = new JPanel();
     Color color = new Color(150,180,200);
     loginPanel.setBackground(color);
     loginPanel.setLayout(null);//can't change button, field and text position without this line!

     

     
     loginText = new JLabel("Welcome! Log in to buy!");
     loginText.setFont(new Font("Monotype Corsiva",20,23));
     loginText.setBounds(40, 20, 270, 25);
     loginPanel.add(loginText);

     //create enter button and add it to the loginPanel.
     enter = new JButton("Enter");
     enter.setBounds(84, 260, 130, 30);
     loginPanel.add(enter);

     //create password field and add it to the loginPanel.
     passwordField = new JPasswordField("", 10);
     passwordField.setBounds(140, 140, 100, 25);
     loginPanel.add(passwordField);


     userNameField = new JTextField("", 10);
     userNameField.setBounds(140, 95, 100, 25);
     loginPanel.add(userNameField);

     userNameText = new JLabel("User name:");
     userNameText.setFont(new Font("Monotype Corsiva",10,20));
     userNameText.setBounds(40, 100, 100, 20);
     loginPanel.add(userNameText);

     passwordText = new JLabel("Password:");
     passwordText.setFont(new Font("Monotype Corsiva",10,20));
     passwordText.setBounds(40, 145, 100, 20);
     loginPanel.add(passwordText);

     frame.add(loginPanel);//add the login panel that we just created to the MyFrame window in MyFrame class.

     enter.addActionListener(this);//listen if we click on enter button.
     userNameField.addActionListener(this);
     passwordField.addActionListener(this);

 }


 public void createProductPanel() {
     productPanel = new JPanel();
     //contentPane.remove(loginPanel);
     frame.remove(loginPanel);
     Color color = new Color(150,180,200);
     productPanel.setBackground(color);
     productPanel.setLayout(null);

     productText = new JLabel("Our Products!");
     productText.setFont(new Font("Monotype Corsiva",20,23));
     productText.setBounds(275, 10, 270, 25);
     productPanel.add(productText);

          productText = new JLabel("In stock:");
     productText.setFont(new Font("Arial",15,15));
     productText.setBounds(275, 48, 270, 25);
     productPanel.add(productText);
             productText = new JLabel("In stock:");
     productText.setFont(new Font("Arial",15,15));
     productText.setBounds(275, 88, 270, 25);
     productPanel.add(productText);
             productText = new JLabel("In stock:");
     productText.setFont(new Font("Arial",15,15));
     productText.setBounds(275, 128, 270, 25);
     productPanel.add(productText);
             productText = new JLabel("In stock:");
     productText.setFont(new Font("Arial",15,15));
     productText.setBounds(275, 168, 270, 25);
     productPanel.add(productText);
             productText = new JLabel("In stock:");
     productText.setFont(new Font("Arial",15,15));
     productText.setBounds(275, 198, 270, 25);
     productPanel.add(productText);

     exit = new JButton("<  Go back");
     exit.setBounds(180, 400, 100, 30);
     productPanel.add(exit);
     
     buyShoes = new JButton("Buy");
     buyShoes.setBounds(400, 50, 100, 20);
     productPanel.add(buyShoes);
   
     buySweater = new JButton("Buy");
     buySweater.setBounds(400, 90, 100, 20);
     productPanel.add(buySweater);
     
     buyJacket = new JButton("Buy");
     buyJacket.setBounds(400, 130, 100, 20);
     productPanel.add(buyJacket);
     
          buyPillow = new JButton("Buy");
     buyPillow.setBounds(400, 170, 100, 20);
     productPanel.add(buyPillow);
     
          buyCandles = new JButton("Buy");
     buyCandles.setBounds(400, 200, 100, 20);
     productPanel.add(buyCandles);
     
   
     
/*   ******************************************************************/
     
     shoes = new JTextField("Nike shoes");
     shoes.setBounds(50, 50, 220, 20);
     shoes.setEditable(false);
     productPanel.add(shoes);
     
       // check with database
      shoesStock = new JTextField(String.format("%d", database.shoesInStock()));
    // shoesStock = new JTextField("4");
      shoesStock.setBounds(330, 50, 30, 20);
     shoesStock.setEditable(false);
     productPanel.add(shoesStock);
     
      
    sweater = new JTextField("sweater");
     sweater.setBounds(50, 90, 220, 20);
     sweater.setEditable(false);
     productPanel.add(sweater);
       
          // check with database
     sweaterStock = new JTextField(String.format("%d", database.sweaterInStock()));
     //sweaterStock = new JTextField("1");
     sweaterStock.setBounds(330, 90, 30, 20);
     sweaterStock.setEditable(false);
     productPanel.add(sweaterStock);
     
       jacket = new JTextField("jacket");
     jacket.setBounds(50, 130, 220, 20);
     jacket.setEditable(false);
     productPanel.add(jacket);
     
          // check with database
          jacketStock = new JTextField(String.format("%d", database.jacketInStock()));
    // jacketStock = new JTextField("1");
     jacketStock.setBounds(330, 130, 30, 20);
     jacketStock.setEditable(false);
     productPanel.add(jacketStock);
     
       pillow = new JTextField("pillow");
     pillow.setBounds(50, 170, 220, 20);
     pillow.setEditable(false);
     productPanel.add(pillow);
     
          // check with database
     pillowStock = new JTextField(String.format("%d", database.pillowInStock()));
   //  pillowStock = new JTextField("1");
     pillowStock.setBounds(330, 170, 30, 20);
     pillowStock.setEditable(false);
     productPanel.add(pillowStock);
     
       scentedCandels = new JTextField("Scented Candels - Orange");
     scentedCandels.setBounds(50, 200,220, 20);
     scentedCandels.setEditable(false);
     productPanel.add(scentedCandels);
     
          // check with database
    scentedCandelsStock = new JTextField(String.format("%d", database.candlesInStock()));
     //scentedCandelsStock = new JTextField("2");
     scentedCandelsStock.setBounds(330, 200,30, 20);
     scentedCandelsStock.setEditable(false);
     productPanel.add(scentedCandelsStock);
     
   

     frame.add(productPanel);//add the product panel that we just created to the MyFrame window in MyFrame class.


     buyShoes.addActionListener(this);
     buySweater.addActionListener(this);
     buyJacket.addActionListener(this);
     buyPillow.addActionListener(this);
     buyCandles.addActionListener(this);
     exit.addActionListener(this);
    
     frame.setVisible(true);
 }

 
 public void actionPerformed(ActionEvent event) {

     
 
      if(event.getSource() == enter) {
          userName = userNameField.getText();
         if(!userName.equals("")){
          System.out.println(userName);
         userPassword= passwordField.getText();
         System.out.println(userPassword);
          
          try {
            Connection c = DriverManager.getConnection("jdbc:mysql://localhost/products", "root", "root");
            Statement st = (Statement) c.createStatement();
   
            String insertDB = "INSERT INTO Members VALUES ("+ database.totalMembersPlusOne() +",'" + userName + "','" + userPassword + "')";  
           
       st.executeUpdate(insertDB); 
            c.close();
        } catch (Exception e) {
            System.err.println("ERROR: " + e);
        }
         }
         
         createProductPanel();
     }
     else if(event.getSource() == exit) {
         //string = String.format(event.getActionCommand());
         System.out.println("Login button OK");
         productPanel.setVisible(false);
         createLoginPanel();

     }
           else if(event.getSource() == buyShoes) {
         //string = String.format(event.getActionCommand());
     JOptionPane.showMessageDialog(null, "Thank you "+userName+ "\nYou bought shoes." );
     database.deleteShoes();
     frame.remove(productPanel);
     createProductPanel();
         
       }
        else if(event.getSource() == buySweater) {
         //string = String.format(event.getActionCommand());
     JOptionPane.showMessageDialog(null, "Thank you "+userName+ "\nYou bought a sweater." );
          database.deleteSweater();
     frame.remove(productPanel);
     createProductPanel();
         
       }
     else if(event.getSource() == buyJacket) {
         //string = String.format(event.getActionCommand());
     JOptionPane.showMessageDialog(null, "Thank you "+userName+ "\nYou bought a jacket." );
          database.deleteJacket();
     frame.remove(productPanel);
     createProductPanel();
     
         
       }
         else if(event.getSource() == buyPillow) {
         //string = String.format(event.getActionCommand());
     JOptionPane.showMessageDialog(null, "Thank you "+userName+ "\nYou bought a pillow." );
          database.deletePillow();
     frame.remove(productPanel);
     createProductPanel();
         
       }
         else if(event.getSource() == buyCandles) {
         //string = String.format(event.getActionCommand());
     JOptionPane.showMessageDialog(null, "Thank you "+userName+ "\nYou bought candles." );
          database.deleteCandle();
     frame.remove(productPanel);
     createProductPanel();
         
       }
 }
}

